/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modelo.persistencia;

import java.util.*;
import java.sql.*;
import modelo.ITarea;
import modelo.Tarea;
/**
 *
 * @author LEONIC
 */
public class TareaDAO implements ITareaDAO{

    public void create(ITarea tarea) {
        String sql = "insert into tarea(tarea,descripcion) values (?,?)";
        try {
            PreparedStatement stm = Persistencia.createConnection().prepareStatement(sql);
            stm.setString(1, tarea.getTarea());
            stm.setString(2, tarea.getDescripcion());

            stm.execute();

        } catch (SQLException e) {
            System.out.println(e);
        } finally {
            Persistencia.closeConnection();
        }
    }

    public ITarea read(String tarea) {
        ITarea c = null;
        try {
            Statement stmt = Persistencia.createConnection().createStatement();
            ResultSet res = stmt.executeQuery("SELECT * FROM tarea where tarea=" + tarea);
            String descrip;
            if (res.next()) {
                descrip = res.getString("descripcion");
                c = new Tarea(tarea,descrip);
            }

        } catch (SQLException e) {
            System.out.println(e);
        } finally {
            Persistencia.closeConnection();
        }
        return c;
    }

    public void update(ITarea tarea) {
        String sql=null;
          sql = "UPDATE tarea SET descripcion = ? WHERE tarea = ?";

          try {
            PreparedStatement stm = Persistencia.createConnection().prepareStatement(sql);
            stm.setString(1, tarea.getDescripcion());
            stm.setString(2, tarea.getTarea());

            stm.execute();

        } catch (SQLException e) {
            System.out.println(e);
        } finally {
            Persistencia.closeConnection();
        }
    }

    public void delete(ITarea tarea) {
        String sql = "DELETE FROM tarea WHERE tarea = ?";
        try {
            PreparedStatement stm = Persistencia.createConnection().prepareStatement(sql);
            stm.setString(1, tarea.getTarea());

            stm.execute();

        } catch (SQLException e) {
            System.out.println(e);
        } finally {
            Persistencia.closeConnection();
        }
    }

    public List<ITarea> list() {
        List<ITarea> tareas = new ArrayList<ITarea>();

        try {
            Statement stmt = Persistencia.createConnection().createStatement();
            ResultSet res = stmt.executeQuery("SELECT * FROM tarea ");
            String tarea,descripcion;
            while (res.next()) {
                tarea = res.getString("tarea");
                descripcion = res.getString("descripcion");
                tareas.add(new Tarea(tarea,descripcion));
            }

        } catch (SQLException e) {
            System.out.println(e);
        } finally {
            Persistencia.closeConnection();
        }
        return tareas;
    }

}
